package com.hspedu.chapter_assignment;

public class HomeWork05 {
    public static void main(String[] args) {
        Person tang = new Person("唐僧", new Horse());
        tang.common();
        tang.passRiver();
        tang.common();
        tang.common();
        tang.passRiver();
        tang.passVolcano();
    }
}

interface Vehicles {
    void work();
}

class Horse implements Vehicles {

    @Override
    public void work() {
        System.out.println("一般情况下，马作为交通工具");
    }
}

class Boat implements Vehicles {

    @Override
    public void work() {
        System.out.println("过河时，船作为交通工具");
    }
}

class Plane implements Vehicles {

    @Override
    public void work() {
        System.out.println("过火山时，飞机作为交通工具");
    }
}

class Person {
    private String name;
    public Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    public void passRiver() {
        if (!(vehicles instanceof Boat)) {
            vehicles = VehiclesFactory.getBoat();
        }

        vehicles.work();
    }

    public void common() {
        if (!(vehicles instanceof Horse)) {
            vehicles = VehiclesFactory.getHorse();
        }

        vehicles.work();
    }

    public void passVolcano() {
        if (!(vehicles instanceof Plane)) {
            vehicles = VehiclesFactory.getPlane();
        }

        vehicles.work();
    }
}

class VehiclesFactory {
    private static Horse house = new Horse();//饿汉式

    private VehiclesFactory() {}

    public static Horse getHorse() {
        return house;
    }

    public static Boat getBoat() {
        return new Boat();
    }

    public static Plane getPlane() {
        return new Plane();
    }
}